﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.Linq;
using System.Transactions;

namespace TradeForm
{
    public partial class FrmTradeForm2 : Form
    {
        private string _defcode = string.Empty;
        public string DefCode
        {
            get { return this._defcode; }
            //  set { this._defcode = value; }
        }
        public bool IsNew = true;

        public FrmTradeForm2()
        {
            InitializeComponent();
            this.IsNew = true;
        }

        public FrmTradeForm2(string defcode)
        {
            InitializeComponent();
            this._defcode = defcode;
            this.IsNew = false;
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            saveBill();
        }

        private bool CheckFormData()
        {
            #region 校验
            if (string.IsNullOrEmpty(this.txtDefCode.Text.Trim()))
            {
                MessageBox.Show("业务编号必填。", "注意", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtDefCode.Focus();
                return;
            }
            if (!string.IsNullOrEmpty(this.txtTotalCount.Text.Trim()))
            {
                try
                {
                    decimal totalCount = Convert.ToDecimal(this.txtTotalCount.Text.Trim());
                }
                catch
                {
                    MessageBox.Show("总件数只能是数字。", "注意", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }

            if (!string.IsNullOrEmpty(this.txtTotalQty.Text.Trim()))
            {
                try
                {
                    decimal totalCount = Convert.ToDecimal(this.txtTotalQty.Text.Trim());
                }
                catch
                {
                    MessageBox.Show("总数量只能是数字。", "注意", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

            }

            if (!string.IsNullOrEmpty(this.txtTotalGrossWeight.Text.Trim()))
            {
                try
                {
                    decimal totalCount = Convert.ToDecimal(this.txtTotalGrossWeight.Text.Trim());
                }
                catch
                {
                    MessageBox.Show("总毛重只能是数字。", "注意", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

            }

            if (!string.IsNullOrEmpty(this.txtTotalMeas.Text.Trim()))
            {
                try
                {
                    decimal totalCount = Convert.ToDecimal(this.txtTotalMeas.Text.Trim());
                }
                catch
                {
                    MessageBox.Show("总体积只能是数字。", "注意", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

            }

            if (!string.IsNullOrEmpty(this.txtTotalNetWeight.Text.Trim()))
            {
                try
                {
                    decimal totalCount = Convert.ToDecimal(this.txtTotalNetWeight.Text.Trim());
                }
                catch
                {
                    MessageBox.Show("总净重只能是数字。", "注意", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

            }
            #endregion
        }

        private void saveBill()
        {
            DataDBDataContext dbcntxt = new DataDBDataContext
                (ConfigurationManager.ConnectionStrings["TradeForm.Properties.Settings.dbConnectionString"].ConnectionString);

            try
            {
                BillExchange billex = new BillExchange();
                ProFormaInvoice pinv = new ProFormaInvoice();
                ProFormaInvoiceGoods pinvGoods = new ProFormaInvoiceGoods();
                Invoice inv = new Invoice();

                bool IsExist = false;

                Transaction ts = new Transaction();
                ts.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
                dbcntxt.Transaction = ts;

                try
                {
                    #region//BillExchange
                    var billexs = from bill in dbcntxt.BillExchange
                                  where bill.DefCode == this._defcode
                                  select bill;
                    if (billexs.Count() > 0)
                    {
                        billex = billexs.First();
                        IsExist = true;
                    }
                    else
                    {
                        IsExist = false;
                    }
                    billex.DefCode = this.txtDefCode.Text;
                    billex.Address = this.txtBiExAddress.Text;
                    billex.BiExNo = this.txtBiExNo.Text;
                    billex.Currency = this.txtBiExCurrency.Text;
                    billex.GreatAmount = Number2English.NumberToString(Convert.ToDouble(this.txtBiExAmount.Text));
                    billex.Interest = this.txtBiExInterest.Text;
                    billex.LCBank = this.txtBiExLCBank.Text;
                    billex.LCDate = this.dtpBiExLCDate.Value.Date;
                    billex.LCno = this.txtBiExLCno.Text;
                    billex.LimitDays = this.txtBiExDays.Text;
                    billex.Payee = this.txtBiExPayee.Text;
                    billex.Payer = this.txtBiExPayer.Text;
                    if (!IsExist)
                    {
                        dbcntxt.BillExchange.InsertOnSubmit(billex);
                    }
                    #endregion
                    #region //ProFormaInvoice
                    var pinvs = from bill in dbcntxt.ProFormaInvoice
                                where bill.DefCode == this.txtDefCode.Text.Trim()
                                select bill;
                    if (pinvs.Count() > 0)
                    {
                        pinv = pinvs.First();
                        IsExist = true;
                    }
                    else
                    {
                        IsExist = false;
                    }
                    pinv.DefCode = this.txtDefCode.Text.Trim();
                    pinv.Consignee = this.txtProFormaConsignee.Text;
                    pinv.DestPort = this.txtProFormaDestPort.Text;
                    pinv.FromPort = this.txtProFormaFromPort.Text;
                    pinv.GatherInfo = this.txtProFormaPayeeInfo.Text;
                    pinv.InvoiceDate = this.dtpProFormaDate.Value.Date;
                    pinv.InvoiceNo = this.txtProFormaInvoiceNo.Text;
                    pinv.Mark = this.txtProFormaRemark.Text;
                    pinv.PriceTerms = this.cmbProFormaPriceTerms.Text;
                    if (!IsExist)
                    {
                        dbcntxt.ProFormaInvoice.InsertOnSubmit(pinv);
                    }
                    #endregion
                    #region //ProFormaInvoiceGoods
                    var pinvGoods = from good in dbcntxt.ProFormaInvoiceGoods
                                    where good.DefCode == this._defcode
                                    select good;
                    foreach (ProFormaInvoiceGoods item in pinvGoods)
                    {
                        dbcntxt.ProFormaInvoiceGoods.DeleteOnSubmit(item);
                    }
                    foreach (DataGridViewRow row in this.dgvProforma.Rows)
                    {
                        if (!row.IsNewRow)
                        {
                            ProFormaInvoiceGoods goods = new ProFormaInvoiceGoods();
                            goods.Amount = row.Cells["ColProformaAmount"].Value == null ? 0.0m : Convert.ToDecimal(row.Cells["ColProformaAmount"].Value);
                            goods.Currency = row.Cells["ColProformaCurrency"].Value == null ? "" : row.Cells["ColProformaCurrency"].Value.ToString();
                            goods.ArtNo = row.Cells["ColProformaArtNo"].Value == null ? "" : row.Cells["ColProformaArtNo"].Value.ToString();
                            goods.ItemNo = row.Index + 1;
                            goods.GoodsName = row.Cells["ColProformaGoodsName"].Value == null ? "" : row.Cells["ColProformaGoodsName"].Value.ToString();
                            goods.Qty = row.Cells["ColProformaQty"].Value == null ? 0.0m : Convert.ToDecimal(row.Cells["ColProformaQty"].Value);
                            goods.UnitPrice = row.Cells["ColProformaUnitPrice"].Value == null ? 0.0m : Convert.ToDecimal(row.Cells["ColProformaUnitPrice"].Value);
                            goods.DefCode = entity.DefCode;
                            goods.QtyUnit = row.Cells["ColProformaQtyUnit"].Value == null ? "" : row.Cells["ColProformaQtyUnit"].Value.ToString();
                            dbcntxt.ProFormaInvoiceGoods.InsertOnSubmit(goods);
                        }
                    }
                    #endregion

                    #region//Invoice
                    var invs = from bill in dbcntxt.Invoice
                               where bill.DefCode == this._defcode
                               select bill;
                    if (invs.Count() > 0)
                    {
                        inv = invs.First();
                        IsExist = true;
                    }
                    else
                    {
                        IsExist = false;
                    }
                    inv.DefCode = this.txtDefCode.Text;
                    inv.Consignee = this.txtProFormaConsignee.Text;
                    inv.ContractNo = this.txtProFormaConsignee.Text;
                    inv.DestPort = this.txtProFormaDestPort.Text;
                    inv.FromPort = this.txtProFormaFromPort.Text;
                    inv.InvoiceDate = this.txtProFormaPayeeInfo.Text;
                    inv.InvoiceNo = this.txtBiExInterest.Text;
                    inv.LCBank = this.txtBiExLCBank.Text;
                    inv.LCDate = this.dtpBiExLCDate.Value.Date;
                    inv.LCno = this.txtBiExLCno.Text;
                    inv.LimitDays = this.txtBiExDays.Text;
                    inv.Payee = this.txtBiExPayee.Text;
                    inv.Payer = this.txtBiExPayer.Text;
                    if (!IsExist)
                    {
                        dbcntxt.Invoice.InsertOnSubmit(inv);
                    }
                    #endregion

                }
                catch (Exception e)
                {

                }





                entity.TotalCount = string.IsNullOrEmpty(this.txtTotalCount.Text) ? 0.0m : Convert.ToDecimal(this.txtTotalCount.Text);
                entity.TotalGrossWeight = string.IsNullOrEmpty(this.txtTotalGrossWeight.Text) ? 0.0m : Convert.ToDecimal(this.txtTotalGrossWeight.Text);
                entity.TotalMeas = string.IsNullOrEmpty(this.txtTotalMeas.Text) ? 0.0m : Convert.ToDecimal(this.txtTotalMeas.Text);
                entity.TotalNetWeight = string.IsNullOrEmpty(this.txtTotalNetWeight.Text) ? 0.0m : Convert.ToDecimal(this.txtTotalNetWeight.Text);
                entity.TotalQty = string.IsNullOrEmpty(this.txtTotalQty.Text) ? 0.0m : Convert.ToDecimal(this.txtTotalQty.Text);

                entity.GreatAmount = this.txtGreatAmount.Text;

                if (string.IsNullOrEmpty(this._defcode))
                {
                    dbcntxt.BillMain.InsertOnSubmit(entity);
                }

                var goodDetails = from good in dbcntxt.Goods
                                  where good.DefCode == entity.DefCode
                                  select good;
                foreach (Goods item in goodDetails)
                {
                    dbcntxt.Goods.DeleteOnSubmit(item);
                }

                dbcntxt.SubmitChanges();
                Goods goods = null;
                foreach (DataGridViewRow row in this.dataGridView1.Rows)
                {
                    if (!row.IsNewRow)
                    {
                        goods = new Goods();
                        goods.Amount = row.Cells["ColAmount"].Value == null ? 0.0m : Convert.ToDecimal(row.Cells["ColAmount"].Value);
                        goods.Currency = row.Cells["ColCurrency"].Value == null ? "" : row.Cells["ColCurrency"].Value.ToString();
                        goods.GoodsEName = row.Cells["ColGoodsEName"].Value == null ? "" : row.Cells["ColGoodsEName"].Value.ToString();
                        goods.ItemNo = row.Index + 1;
                        goods.HSCode = row.Cells["ColHScode"].Value == null ? "" : row.Cells["ColHScode"].Value.ToString();
                        goods.GoodsName = row.Cells["ColGoodsName"].Value == null ? "" : row.Cells["ColGoodsName"].Value.ToString();
                        goods.Qty = row.Cells["ColQty"].Value == null ? 0.0m : Convert.ToDecimal(row.Cells["ColQty"].Value);
                        goods.UnitPrice = row.Cells["ColUnitPrice"].Value == null ? 0.0m : Convert.ToDecimal(row.Cells["ColUnitPrice"].Value);
                        goods.Measures = row.Cells["ColMeasures"].Value == null ? 0.0m : Convert.ToDecimal(row.Cells["ColMeasures"].Value);
                        goods.GrossWeight = row.Cells["ColGrossWeight"].Value == null ? 0.0m : Convert.ToDecimal(row.Cells["ColGrossWeight"].Value);
                        goods.NetWeight = row.Cells["ColNetWeight"].Value == null ? 0.0m : Convert.ToDecimal(row.Cells["ColNetWeight"].Value);
                        goods.DefCode = entity.DefCode;
                        goods.QtyUnit = row.Cells["ColQtyUnit"].Value == null ? "" : row.Cells["ColQtyUnit"].Value.ToString();
                        dbcntxt.Goods.InsertOnSubmit(goods);
                    }
                }

                var packlists = from pack in dbcntxt.PackList
                                where pack.DefCode == entity.DefCode
                                select pack;
                foreach (PackList item in packlists)
                {
                    dbcntxt.PackList.DeleteOnSubmit(item);
                }

                dbcntxt.SubmitChanges();
                PackList packList = null;
                foreach (DataGridViewRow row in this.gdvPacklist.Rows)
                {
                    if (!row.IsNewRow)
                    {
                        packList = new PackList();
                        packList.ArtNo = row.Cells["ColArtNo"].Value == null ? "" : row.Cells["ColArtNo"].Value.ToString();
                        packList.ItemNo = row.Index + 1;

                        packList.CaseNo = row.Cells["ColCaseNo"].Value == null ? "" : row.Cells["ColCaseNo"].Value.ToString();
                        packList.ColorCN = row.Cells["ColColorCN"].Value == null ? "" : row.Cells["ColColorCN"].Value.ToString();
                        packList.ColorEN = row.Cells["ColColorEN"].Value == null ? "" : row.Cells["ColColorEN"].Value.ToString();
                        packList.Ctns = row.Cells["ColCtns"].Value == null ? "" : row.Cells["ColCtns"].Value.ToString();
                        packList.GrossWeight = row.Cells["ColPackGrossWeight"].Value == null ? "" : row.Cells["ColPackGrossWeight"].Value.ToString();
                        packList.Meas = row.Cells["ColPackMeas"].Value == null ? "" : row.Cells["ColPackMeas"].Value.ToString();
                        packList.NetWeight = row.Cells["ColPackNetWeight"].Value == null ? "" : row.Cells["ColPackNetWeight"].Value.ToString();
                        packList.PONo = row.Cells["ColPONo"].Value == null ? "" : row.Cells["ColPONo"].Value.ToString();
                        packList.Qty = row.Cells["ColQnty"].Value == null ? "" : row.Cells["ColQnty"].Value.ToString();
                        packList.Size10 = row.Cells["ColSize10"].Value == null ? "" : row.Cells["ColSize10"].Value.ToString();
                        packList.Size11 = row.Cells["ColSize11"].Value == null ? "" : row.Cells["ColSize11"].Value.ToString();
                        packList.Size12 = row.Cells["ColSize12"].Value == null ? "" : row.Cells["ColSize12"].Value.ToString();
                        packList.Size13 = row.Cells["ColSize13"].Value == null ? "" : row.Cells["ColSize13"].Value.ToString();
                        packList.Size14 = row.Cells["ColSize14"].Value == null ? "" : row.Cells["ColSize14"].Value.ToString();
                        packList.Size15 = row.Cells["ColSize15"].Value == null ? "" : row.Cells["ColSize15"].Value.ToString();
                        packList.Size5 = row.Cells["ColSize5"].Value == null ? "" : row.Cells["ColSize5"].Value.ToString();
                        packList.Size6 = row.Cells["ColSize6"].Value == null ? "" : row.Cells["ColSize6"].Value.ToString();
                        packList.Size7 = row.Cells["ColSize7"].Value == null ? "" : row.Cells["ColSize7"].Value.ToString();
                        packList.Size8 = row.Cells["ColSize8"].Value == null ? "" : row.Cells["ColSize8"].Value.ToString();
                        packList.Size9 = row.Cells["ColSize9"].Value == null ? "" : row.Cells["ColSize9"].Value.ToString();
                        packList.SizeM = row.Cells["ColSizeM"].Value == null ? "" : row.Cells["ColSizeM"].Value.ToString();
                        packList.StockNo = row.Cells["ColStockNo"].Value == null ? "" : row.Cells["ColStockNo"].Value.ToString();

                        packList.DefCode = entity.DefCode;
                        dbcntxt.PackList.InsertOnSubmit(packList);
                    }
                }


                dbcntxt.SubmitChanges(ConflictMode.FailOnFirstConflict);

            }
            catch (Exception ex)
            {
                MessageBox.Show("保存失败。");
                return;
            }
            MessageBox.Show("保存成功。");
            this.IsNew = false;
            this._defcode = entity.DefCode;
        }

        private void showData(string defcode)
        {
            DataDBDataContext dbcntxt = new DataDBDataContext
                    (ConfigurationManager.ConnectionStrings["TradeForm.Properties.Settings.dbConnectionString"].ConnectionString);
            // BillMain entity = new BillMain();
            BillMain entity = null;
            var items = from billmain in dbcntxt.BillMain
                        where billmain.DefCode == defcode
                        select billmain;
            foreach (var item in items)
            {
                if (item != null)
                {
                    entity = (BillMain)item;
                    this.txtCustBillLadingNo.Text = entity.BillLadingNo;
                    this.txtCancelVeriNo.Text = entity.CancelVeriNo;
                    this.txtCustBillLadingNo.Text = entity.BillLadingNo;
                    this.chkFreCanBatch.Checked = entity.CanBatch.Value;
                    this.chkFreCanTranshipment.Checked = entity.CanTranshipment.Value;
                    this.txtCancelVeriNo.Text = entity.CancelVeriNo;
                    this.txtCarrier.Text = entity.Carrier;
                    this.txtCertNo.Text = entity.CertNo;
                    this.txtConsignee.Text = entity.Consignee;
                    this.txtConsignmentBillNo.Text = entity.ConsignmentBillNo;
                    this.txtConsignmentCode.Text = entity.ConsignmentCode;
                    this.txtConsignor.Text = entity.Consignor;
                    this.txtCustContainnerNO.Text = entity.ContainnerNO;
                    this.txtContContractNo.Text = entity.ContractNo;
                    this.txtConveyance.Text = entity.Conveyance;
                    this.txtConveyanceNo.Text = entity.ConveyanceNo;
                    this.dtpCustCustomDate.Value = entity.CustomDate.Value;
                    this.txtDefCode.Text = entity.DefCode;
                    this.txtDestPort.Text = entity.DestPort;
                    this.dtpCustExportDate.Value = entity.ExpDate.Value;
                    this.dtpFormDate.Value = entity.FormDate.Value;
                    this.txtFreightPay.Text = entity.FreightPay;
                    this.txtFromPort.Text = entity.FromPort;
                    this.txtCustInsurance.Text = entity.Insurance;
                    this.dtpInvoiceDate.Value = entity.InvoiceDate.Value;
                    this.txtInvoiceNo.Text = entity.InvoiceNo;
                    this.txtBiExLCBank.Text = entity.LCBank;
                    this.dtpLCDate.Value = entity.LCDate.Value;
                    this.txtBiExLCno.Text = entity.LCno;
                    this.txtCustManufacture.Text = entity.Manufacture;
                    this.txtCustMark.Text = entity.Mark;
                    this.txtOperateCompany.Text = entity.OperateCompany;
                    this.txtPackMethod.Text = entity.PackMethod;  //装箱方式
                    this.txtBiExPayer.Text = entity.Payer;
                    this.txtBiExExporter.Text = entity.PaymentBank;
                    this.txtPaymentMethod.Text = entity.PaymentMethod;
                    this.cmbPriceTerms.Text = entity.PriceTerms;
                    this.cmbCustRemittanceWay.Text = entity.Remittanceway;
                    this.dtpFreShipmentDate.Value = entity.ShipmentDate.Value;
                    this.txtShipName.Text = entity.ShipName;
                    this.txtCustTaxDutyFree.Text = entity.TaxDutyFree;
                    this.txtTotalCount.Text = entity.TotalCount.ToString();
                    this.txtTotalGrossWeight.Text = entity.TotalGrossWeight.ToString();
                    this.txtTotalMeas.Text = entity.TotalMeas.ToString();
                    this.txtTotalNetWeight.Text = entity.TotalNetWeight.Value.ToString();
                    this.txtTotalQty.Text = entity.TotalQty.Value.ToString();
                    this.txtCustTradeCountry.Text = entity.TradeCountry;
                    this.txtTradeKind.Text = entity.TradeKind;
                    this.txtTranshipmentPort.Text = entity.TranshipmentPort;
                    this.txtCustTransportMode.Text = entity.TransportMode;
                    this.txtCustWarrantNo.Text = entity.WarrantNo;

                    this.txtCustRecordNo.Text = entity.RecordNo;
                    this.txtCustApprovalNo.Text = entity.ApprovalNo;
                    this.txtCustTurnoverWay.Text = entity.TurnoverWay;
                    this.txCustTransportFees.Text = entity.MiscellaneousFees;
                    this.txtCustAttachFile.Text = entity.AttachFile;
                    this.txtCustTax.Text = entity.Tax;
                    this.txtPackType.Text = entity.PackType; //包装种类
                    this.txtCustomNo.Text = entity.CustomNo;
                    this.txtCustomPreNo.Text = entity.CustomPreNo;
                    this.txtCustGoodsFrom.Text = entity.GoodsFrom;
                    this.txtMembersDeclaration.Text = entity.MembersDeclaration;
                    this.txtTyper.Text = entity.Typer;
                    this.txtTypeCompany.Text = entity.TypeCompany;

                    this.txtPayee.Text = entity.Payee;
                    this.txtPayeeACNo.Text = entity.PayeeACNo;
                    this.txtPayeeBank.Text = entity.PayeeBank;
                    this.txtPayeeSwiftCode.Text = entity.PayeeSwiftCode;

                    this.txtTotalCName.Text = entity.TotalCName;
                    this.txtTotalEName.Text = entity.TotalEName;
                    this.txtCustOtherFees.Text = entity.Freight;
                    this.txtFreNotifyParty.Text = entity.NotifyParty;

                    this.dtpBiExLCDate.Value = entity.PODate.HasValue ? entity.PODate.Value : DateTime.Now;
                    this.txtBiExDays.Text = entity.PODays;
                    this.txtBiExInterest.Text = entity.POInterest;
                    this.txtBiExNo.Text = entity.PONo;
                    this.txtGreatAmount.Text = entity.GreatAmount;

                    this.txtDefCode.ReadOnly = true;

                }
            }
            var goods = from good in dbcntxt.Goods
                        where good.DefCode == defcode
                        select good;
            this.dataGridView1.DataSource = goods;

            var packs = from pack in dbcntxt.PackList
                        where pack.DefCode == defcode
                        select pack;
            this.gdvPacklist.DataSource = packs;
        }

        private void FrmTradeForm_Load(object sender, EventArgs e)
        {
            this.dataGridView1.AutoGenerateColumns = false;
            this.gdvPacklist.AutoGenerateColumns = false;
            if (!string.IsNullOrEmpty(this._defcode))
            {
                showData(this._defcode);
            }
            if (IsNew)
            {
                this.Text = "新建单证";
            }
            else
            {
                this.Text = "编辑单证";
            }
        }

        //private void cmbDestPort_KeyPress(object sender, KeyPressEventArgs e)
        //{
        //    if (e.KeyChar == (char)Keys.Enter)
        //    {
        //        ComboBox cmb = (ComboBox)sender;
        //        if (!string.IsNullOrEmpty(cmb.Text))
        //        {
        //            DataDBDataContext dbcntxt = new DataDBDataContext
        //           (ConfigurationManager.ConnectionStrings["TradeForm.Properties.Settings.dbConnectionString"].ConnectionString);
        //            var ports = from port in dbcntxt.Port
        //                        where port.PortCode.Contains(cmb.Text)
        //                        select port;
        //            DataTable DataTable1 = new DataTable();

        //            DataTable1.Columns.Add("PortCode", typeof(string));
        //            DataTable1.Columns.Add("CName", typeof(string));
        //            cmb.BeginUpdate();
        //            foreach (var item in ports)
        //            {
        //                Port port = (Port)item;
        //                DataTable1.Rows.Add(port.PortCode, port.CName);
        //            }
        //            cmb.DisplayMember = "CName";
        //            cmb.ValueMember = "PortCode";
        //            cmb.DataSource = DataTable1;
        //            cmb.EndUpdate();
        //            //cmb.SelectedIndex = 1;
        //            //cmb.DroppedDown = true;
        //            // cmb.DropDownStyle = ComboBoxStyle.DropDownList;
        //        }
        //        else
        //        {
        //            this.SelectNextControl(this.ActiveControl, true, true, false, true);
        //        }
        //        //e.Handled = true;
        //    }
        //}



        private void btnSelectDestPort_Click(object sender, EventArgs e)
        {
            FrmCode frm = new FrmCode();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                this.txtDestPort.Text = frm.ReturnName;
            }
        }

        private void btnSelectFromPort_Click(object sender, EventArgs e)
        {
            FrmCode frm = new FrmCode();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                this.txtFromPort.Text = frm.ReturnName;
            }
        }

        private void btnSelectTransPort_Click(object sender, EventArgs e)
        {
            FrmCode frm = new FrmCode();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                this.txtTranshipmentPort.Text = frm.ReturnName;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (this.IsNew)
            {
                MessageBox.Show("先保存数据。", "注意", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                FrmPrint fr = new FrmPrint(this.txtDefCode.Text);
                //fr.MdiParent = this.MdiParent;
                fr.ShowDialog();
            }

        }

        private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {

            DataGridView gdv = (DataGridView)sender;
            gdv.Rows[e.RowIndex].ErrorText = "";
            if (e.FormattedValue != null && !string.IsNullOrEmpty(e.FormattedValue.ToString()) &&
                (gdv.Columns[e.ColumnIndex].Name == "ColQty" || gdv.Columns[e.ColumnIndex].Name == "ColUnitPrice" ||
                gdv.Columns[e.ColumnIndex].Name == "ColAmount" || gdv.Columns[e.ColumnIndex].Name == "ColMeasures" ||
                gdv.Columns[e.ColumnIndex].Name == "ColGrossWeight" || gdv.Columns[e.ColumnIndex].Name == "ColNetWeight"))
            {
                if (dataGridView1.Rows[e.RowIndex].IsNewRow) { return; }
                try
                {
                    decimal cellValue = Convert.ToDecimal(e.FormattedValue);
                }
                catch
                {
                    e.Cancel = true;
                    gdv.Rows[e.RowIndex].ErrorText = "只能输入数字";

                }
            }
        }






    }
}
